package com.deworks.admin.db.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.google.common.collect.ImmutableMap;

import java.nio.file.Paths;
import java.sql.Types;
import java.util.Collections;

public class CodeGenerator {
    public static void main(String[] args) {
        String rootPath = Paths.get(System.getProperty("user.dir")).toAbsolutePath().toString();
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/db_admin", "dev", "123456")
                .globalConfig(builder -> {
                    builder.author("liu_lin"); // 设置作者

                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder ->
                        builder.parent("com.deworks.admin") // 设置父包名
                                .entity("entity")
                                .mapper("dao")
                                .service("service")
                                .serviceImpl("service.impl")
                                .pathInfo(ImmutableMap.of(OutputFile.entity, rootPath+"\\app-domain\\src\\main\\java\\com\\deworks\\admin\\entity",
                                        OutputFile.mapper, rootPath+"\\app-infrastructure\\src\\main\\java\\com\\deworks\\admin\\dao",
                                        OutputFile.service, rootPath+"\\app-infrastructure\\src\\main\\java\\com\\deworks\\admin\\service",
                                        OutputFile.serviceImpl, rootPath+"\\app-infrastructure\\src\\main\\java\\com\\deworks\\admin\\service\\impl"
                                        ))
                )
                .strategyConfig(builder ->
                        builder.addInclude("at_user") // 设置需要生成的表名
                                .addTablePrefix("t_", "c_") // 设置过滤表前缀
                )
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
